#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年5月12日

@author: chenyongbing
'''
import sys,os,re,time
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

import logging



class CompressRawdata(object):
    '''
        压缩rawdata
    '''
    def __init__(self):
        '''
        Constructor
        '''
        
    def listCallback(self,result=[]):  
        """
                    拿到所有线程执行完返回的结果
        """
        total_time = [r[1][0] for r in result]
        total_count = [r[1][1] for r in result]
#         print total_time
#         print total_count
        print sum(total_count),sum(total_time)
        reactor.stop()
        
        
    def read_in_chunks_old(self,rawfile, chunk_size=1024*1024):
        """
        Lazy function (generator) to read a file piece by piece.
        Default chunk size: 1M
        You can set your own chunk size 
        """
        file_object = open(rawfile)
        while True:
            chunk_data = file_object.read(chunk_size)
            if not chunk_data:
                break
            yield chunk_data
    
    def read_in_chunks(self,rawfile, chunk_size=1024*1024):
        """
        Lazy function (generator) to read a file piece by piece.
        Default chunk size: 1M
        You can set your own chunk size 
        """
        file_object = open(rawfile)
        while True:
            chunk_data = file_object.readlines(chunk_size)
            if not chunk_data:
                break
            yield chunk_data
     
    def read_rawdata_by_line(self,rawfile=''):
        datas = {}
        try:
            today = re.search('([\d]{4}-[\d]{2}-[\d]{2})',rawfile).group(1)
        except:
            sys.exit()
        fr = open(rawfile)
        BUFSIZE = 1024000
        lines = fr.readlines(BUFSIZE) 
        i=0 
        while lines:  
            i+=1
            for line in lines:  
                #print line
                day,device,mac,stime,rssi,rtime = line.split('.')
                if day!=today:continue
                if device not in datas.keys():
                    datas[device]= []
#                 if mac not in datas[device].keys():
#                     datas[device][mac] = []
#                 #data = datas[device][mac]
                datas[device].append('%s:%s:%s'%(mac,stime,rssi))
                
            print i,len(lines),len(datas)
            lines = fr.readlines(BUFSIZE) 
    
    def read_line(self,output=''):
        datas = {}
        out = ''
        total=0
        i=0
        #fr = open('../log/new.txt','a+')
        for line in output:
            lineList = line.split('.')
            if len(lineList)==0:continue
            if len(lineList)!=6:
                continue
            i+=1
            st = time.time()
            day,device,mac,stime,rssi,rtime = lineList
            #fr.write('%s.%s.%s.%s\n'%(device,mac,stime,rssi))
            #rConnect.lpush('%s-%s'%(device,mac),'%s-%s'%(stime,rssi))

            et = time.time()
            total += et-st
#             if device not in datas.keys():
#                 datas[device]= {}
#             if mac not in datas[device].keys():
#                 datas[device][mac]= []
#             
#             data  = datas.keys()
#             if mac not in data:
#                 datas[mac] = []
#             datas[mac].append('%s:%s'%(stime,rssi))
        
        print total,i
        return total,i
    
from mrjob.job import MRJob
class MRRawData(MRJob):    

    def get_time_stamp(self,stime):
        tlist = stime.split(':')
        stamp = int(tlist[0])*3600+int(tlist[1])*60+int(tlist[2])
        return stamp
    def mapper(self, _, line):
        lineList = line.split('.')
        if len(lineList)==6:
            day,device,mac,stime,rssi,rtime = lineList
            stamp = self.get_time_stamp(stime)
            yield '%s-%s'%(device,mac),(stamp,rssi)
            #yield device,1
#     def combiner(self,key,values):
#         datas = []
#         value = values.next()
#         while value:
#             value = values.next()
#             datas.append(value)
#         yield (key,len(datas))
#         yield (key,sum(values))
#     
    
    def reducer(self,key,values):
        datas = []
        for stamp,rssi in values:
            datas.append('%s.%s'%(stamp,rssi))
        yield (key,','.join(datas))
      
    
if __name__ == '__main__':
    st = time.time()
    MRRawData.run()
    et= time.time()
    
    logging.error( "Usetime: %s"%(et-st))
